package ship.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import ship.entity.usertable;
import ship.service.shipservice;

/**
 * Servlet implementation class addusertableServlet
 */
@WebServlet("/addusertableServlet")
public class addusertableServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public addusertableServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("utf-8");
		int id = Integer.parseInt( request.getParameter("sid"));
		String name = request.getParameter("sname");
		String email = request.getParameter("semail");
		String age = request.getParameter("sage");
		String hobby = request.getParameter("shobby");
		String ct = request.getParameter("sct");
	    String address = request.getParameter("saddress");
	    usertable user = new usertable(id,name,email,age,hobby,ct,address);
	    shipservice service = new shipservice();
	    boolean result = service.addusertable(user);
		/*
		 * //设置响应编码 response.setContentType("text/html; charset=utf-8");
		 * response.setCharacterEncoding("utf-8"); PrintWriter out =
		 * response.getWriter();
		 */
	    if(result) {
            String mess = "添加信息成功!";
            request.setAttribute("mess", mess); 
            request.getRequestDispatcher("queryallusertableServlet").forward(request, response);
	    }else {
            String mess = "添加信息失败，此用户已存在!";
            request.setAttribute("mess", mess); 
            request.getRequestDispatcher("queryallusertableServlet").forward(request, response);
    		
	}
}	

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("utf-8");
		int id = Integer.parseInt( request.getParameter("sid"));
		String name = request.getParameter("sname");
		String email = request.getParameter("semail");
		String age = request.getParameter("sage");
		String hobby = request.getParameter("shobby");
		String ct = request.getParameter("sct");
	    String address = request.getParameter("saddress");
	    usertable user = new usertable(id,name,email,age,hobby,ct,address);
	    shipservice service = new shipservice();
	    boolean result = service.addusertable(user);
		/*
		 * //设置响应编码 response.setContentType("text/html; charset=utf-8");
		 * response.setCharacterEncoding("utf-8"); PrintWriter out =
		 * response.getWriter();
		 */
	    if(result) {
            String mess = "添加用户信息成功!";
            request.setAttribute("mess", mess); 
            request.getRequestDispatcher("queryallusertableServlet").forward(request, response);
	    }else {
            String mess = "添加信息失败，此用户已存在!";
            request.setAttribute("mess", mess); 
            request.getRequestDispatcher("queryallusertableServlet").forward(request, response);
    		
	}
	}

}
